package cn.njxzc.examples;

class CubbyHole {
/*	private int content;
	public int get() {
		return content;
	}
	public void put(int value) {
		content = value;
	}
*/
	private int content; 
	public synchronized int get() { 
	return content;  
	}
public synchronized void put(int value) {
content = value; 

}
}
class Producer extends Thread {
	private CubbyHole cubbyhole;

	public Producer(CubbyHole c) {
		cubbyhole = c;
	}

	public void run() {
		for (int i = 0; i < 10; i++) {
			cubbyhole.put(i);
			System.out.println("Producer " + " put:" + i);
			try {
				sleep((int) (Math.random() * 10));
			} catch (InterruptedException e) {
				System.out.println(e);
			}
		}
	}
}

class Consumer extends Thread {
	private CubbyHole cubbyhole;

	public Consumer(CubbyHole c) {
		cubbyhole = c;
	}

	public void run() {
		int value = 0;
		for (int i = 0; i < 10; i++) {
			value = cubbyhole.get();
			System.out.println("Consumer " + " got: " + value);
		}
	}
}

public class Exam7_6 {
	public static void main(String[] args) {
		CubbyHole h = new CubbyHole();
		Producer p = new Producer(h);
		Consumer c = new Consumer(h);
		p.start();
		c.start();
	}
}
